package com.jfl.web.controller.system;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysPost;
import com.jfl.system.SysPostService;
import com.jfl.utils.ConvertRequestUtil;

/**
 * 岗位管理
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月20日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/post")
public class SysPostController extends AbstractController
{
    
    @Reference(version = "${jfl.version}")
    private SysPostService sysPostService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /**
     * 菜单跳转页面
     */
    @RequiresPermissions("system:post:view")
    @GetMapping
    public String post(ModelMap modelMap)
    {
        return Constants.MODULE_SYS_POST_PREFIX + "post";
    }
    
    /**
     * 分页查询
     * 
     * @return
     */
    @RequiresPermissions("system:post:list")
    @PostMapping("/list")
    @ResponseBody
    public ResponseEntity<ModelMap> list(SysPost sysPost)
    {
        PageInfo<SysPost> pageInfo = this.sysPostService.selectList(ConvertRequestUtil.pageRequest(), sysPost);
        return setSuccessModelMap(pageInfo);
    }
    
    /**
     * 修改
     * 
     * @return
     */
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap modelMap)
    {
        modelMap.put("post", this.sysPostService.queryById(postId));
        return Constants.MODULE_SYS_POST_PREFIX + "edit";
    }
    
    /**
     * 跳转页面
     * 
     * @param sysMenu
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        return Constants.MODULE_SYS_POST_PREFIX + "add";
    }
    
    /**
     * 跳转页面
     * 
     * @param sysMenu
     * @return
     */
    @Log(module = Module.POST, value = "添加岗位", type = LogType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> add(SysPost sysPost)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        // 设置创建人
        sysPost.setCreateBy(ShiroUtils.getCurrentUserId());
        return setSuccessModelMap(this.sysPostService.update(sysPost));
    }
    
    /**
     * 删除岗位 支持批量删除
     * 
     * @param ids
     * @return
     */
    @Log(module = Module.POST, value = "删除岗位", type = LogType.DELETE)
    @RequiresPermissions("system:post:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResponseEntity<ModelMap> remove(String ids)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysPostService.deleteByIds(ids);
        return setSuccessModelMap();
    }
    
    /**
     * 修改保存岗位
     */
    @Log(module = Module.POST, value = "修改岗位", type = LogType.UPDATE)
    @RequiresPermissions("system:post:edit")
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> editSave(SysPost post)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        post.setUpdateBy(ShiroUtils.getCurrentUserId());
        this.sysPostService.update(post);
        return setSuccessModelMap();
    }
    
    /**
     * 校验岗位名称
     * 
     * @param postName
     * @return
     */
    @PostMapping("/checkPostNameUnique")
    @ResponseBody
    public String checkPostNameUnique(SysPost post)
    {
        return this.sysPostService.checkPostNameUnique(post);
    }
    
    /**
     * 校验岗位编码
     * 
     * @param postCode
     * @return
     */
    @PostMapping("/checkPostCodeUnique")
    @ResponseBody
    public String checkPostCodeUnique(SysPost post)
    {
        return this.sysPostService.checkPostCodeUnique(post);
    }
}
